package influxUtils

import (
	"context"
	influxdb2 "github.com/influxdata/influxdb-client-go/v2"
	"github.com/influxdata/influxdb-client-go/v2/api"
	"github.com/influxdata/influxdb-client-go/v2/api/write"
)

/*
influxDB名词
database：数据库；
measurement：数据库中的表；
points：表里面的一行数据。
*/
//var url = "http://192.168.20.9:8086"

type Client struct {
	bucket, org string
	client      influxdb2.Client
	writeAPI    api.WriteAPI
	queryAPI    api.QueryAPI
}

func NewInfluxClient(url, token string) *Client {
	return &Client{
		client: influxdb2.NewClientWithOptions(url, token, influxdb2.DefaultOptions().SetBatchSize(20)),
	}
}

func (c *Client) Init(bucket, org string) *Client {
	c.org = org
	c.bucket = bucket
	c.writeAPI = c.client.WriteAPI(c.org, c.bucket)
	c.queryAPI = c.client.QueryAPI(c.org)
	return c
}

func (c *Client) Write(p *write.Point) {
	c.writeAPI.WritePoint(p)
}
func (c *Client) Del(ctx context.Context, p *write.Point) {
	c.Del(ctx, p)
}
